#define _CRT_SECURE_NO_WARNINGS

#include "file_op.h"
#include "config.h"
#include "network.h"
#include "nef.h"

#include "dump.h"

void dump_csv_ne_nf()
{
    //Get the output file path.
    char path_ne_nf_csv[256];
    generate_output_path("ne_nf.csv", path_ne_nf_csv);
    //Open the file as write data.
    FILE *ne_nf_csv = open_file_write(path_ne_nf_csv,
                                      "Failed to open NE/NF csv dump file.");
    //Prepare the compound concentration coordinate.
    double *cc_coordinate = new double[num_of_compounds];
    //Loop and calculate the coordinate.
    for(int i=0; i<ne_nf_length; ++i)
    {
        //Calculate the concentration coordinate.
        global_index_to_concentration(i, cc_coordinate);
        //Loop and print concentration of all compounds.
        for(int j=0; j<num_of_compounds; ++j)
        {
            fprintf(ne_nf_csv, "%lf,", cc_coordinate[j]);
        }
        //Get the NE/NF pos.
        double *ne_nf_block = NE_NF_results + i * 2;
        //Print the NE and NF value, first is NE, second is NF.
        fprintf(ne_nf_csv, "%lf,%lf\n", ne_nf_block[0], ne_nf_block[1]);
    }
    delete[] cc_coordinate;
    //Close the file for finish flush.
    fclose(ne_nf_csv);
}

void dump_csv_nef()
{
    //Get the output file path.
    char path_ne_nf_csv[256];
    generate_output_path("nef.csv", path_ne_nf_csv);
    //Open the file as write data.
    FILE *ne_nf_csv = open_file_write(path_ne_nf_csv,
                                      "Failed to open NE/NF csv dump file.");
    //Prepare the compound concentration coordinate.
    double *cc_coordinate = new double[num_of_compounds];
    //Loop and calculate the coordinate.
    for(int i=0; i<ne_nf_length; ++i)
    {
        //Calculate the concentration coordinate.
        global_index_to_concentration(i, cc_coordinate);
        //Loop and print concentration of all compounds.
        for(int j=0; j<num_of_compounds; ++j)
        {
            fprintf(ne_nf_csv, "%lf,", cc_coordinate[j]);
        }
        //Get the NE/NF pos.
        double *ne_nf_block = NE_NF_results + i * 2;
        //Print the NEF value, it is the original NE position.
        fprintf(ne_nf_csv, "%lf\n", ne_nf_block[0]);
    }
    delete[] cc_coordinate;
    //Close the file for finish flush.
    fclose(ne_nf_csv);
}

void dump_csv_ec()
{
    char ec_filename[256];
    char ec_value[256];
    char ec_filepath[256];
    //Prepare the compound concentration coordinate.
    double *cc_coordinate = new double[num_of_compounds];
    //Loop for all the EC value.
    for(int m=0; m<ec_list.size(); ++m)
    {
        //Get the EC value.
        strcpy(ec_filename, "ec_");
        sprintf(ec_value, "%.2lf", ec_list.at(m) * 100.0);
        strcat(ec_filename, ec_value);
        strcat(ec_filename, ".csv");
        //Generate the csv file path.
        generate_output_path(ec_filename, ec_filepath);
        //Open the file as write.
        FILE *ec_csv = open_file_write(ec_filepath,
                                       "Failed to open EC csv dump file.");
        //Now loop for the list.
        for(int i=0; i<ec_index_lists[m].size(); ++i)
        {
            //Calculate the concentration coordinate.
            global_index_to_concentration(ec_index_lists[m].at(i),
                                          cc_coordinate);
            //Loop and print concentration of all compounds.
            for(int j=0; j<num_of_compounds; ++j)
            {
                //Check the value index.
                if(j>0)
                {
                    //Print column first.
                    fprintf(ec_csv, ",%lf", cc_coordinate[j]);
                }
                else
                {
                    //Only print the value.
                    fprintf(ec_csv, "%lf", cc_coordinate[j]);
                }
            }
            fprintf(ec_csv, "\n");
        }
        fclose(ec_csv);
    }
    //Recover memory.
    delete[] cc_coordinate;
}

void dump_single_csv_ne_nf()
{
    //Get the output file path.
    char path_ne_nf_csv[256];
    //Loop for all the compounds.
    for(int m=0; m<num_of_compounds; ++m)
    {
        //Construct the name.
        char compound_id[14];
        _itoa(m, compound_id, 10);
        char filename[256];
        strcpy(filename, "ne_nf_single_");
        strcat(filename, compound_id);
        strcat(filename, ".csv");
        // Generate the file path.
        generate_output_path(filename, path_ne_nf_csv);
        //Open the file as write.
        FILE *single_ne_nf_csv =
                open_file_write(path_ne_nf_csv,
                                "Failed to open single compound csv dump file.");
        //Get the destination address.
        double *single_result = single_NE_NF_results + m * single_axis_length * 2;
        // Loop and print the concentration to CSV file first.
        for(int i=0; i<single_axis_length; ++i)
        {
            //Get the NE/NF pointer.
            double *ne_nf_block = single_result + i * 2;
            //Print the concentration.
            fprintf(single_ne_nf_csv, "%lf,%lf,%lf\n",
                    single_axis_c[i], ne_nf_block[0], ne_nf_block[1]);
        }
        fclose(single_ne_nf_csv);
    }
}

void dump_single_csv_nef()
{
    //Get the output file path.
    char path_ne_nf_csv[256];
    //Loop for all the compounds.
    for(int m=0; m<num_of_compounds; ++m)
    {
        //Construct the name.
        char compound_id[14];
        _itoa(m, compound_id, 10);
        char filename[256];
        strcpy(filename, "nef_single_");
        strcat(filename, compound_id);
        strcat(filename, ".csv");
        // Generate the file path.
        generate_output_path(filename, path_ne_nf_csv);
        //Open the file as write.
        FILE *single_nef_csv =
                open_file_write(path_ne_nf_csv,
                                "Failed to open single compound csv dump file.");
        //Get the destination address.
        double *single_result = single_NE_NF_results + m * single_axis_length * 2;
        // Loop and print the concentration to CSV file first.
        for(int i=0; i<single_axis_length; ++i)
        {
            //Get the NE/NF pointer.
            double *ne_nf_block = single_result + i * 2;
            //Print the concentration.
            fprintf(single_nef_csv, "%lf,%lf\n",
                    single_axis_c[i], ne_nf_block[0]);
        }
        fclose(single_nef_csv);
    }
}

void dump_single_csv_ec()
{
    char ec_filename[256];
    char ec_value[256];
    char ec_filepath[256];
    //Prepare the compound concentration coordinate.
    double *cc_coordinate = new double[num_of_compounds];
    //Loop for all the compound.
    for(int ci=0; ci<num_of_compounds; ++ci)
    {
        //Get the EC value.
        strcpy(ec_filename, "single_");
        _itoa(ci, ec_value, 10);
        strcat(ec_filename, ec_value);
        strcat(ec_filename, "_ec.csv");
        //Generate the csv file path.
        generate_output_path(ec_filename, ec_filepath);
        //Open the file as write.
        FILE *ec_csv = open_file_write(ec_filepath,
                                       "Failed to open single EC csv dump file.");
        //Loop for all the EC value, print into a CSV file.
        for(int m=0; m<ec_list.size(); ++m)
        {
            fprintf(ec_csv, "%lf,%lf\n", ec_list.at(m), single_ec_lists[ci][m]);
        }
        fclose(ec_csv);
    }
    //Recover memory.
    delete[] cc_coordinate;
}
